#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define CTRL_LED_CMD    (0x01)

typedef struct devinfo
{
    unsigned int which;
    unsigned int onoff;
}devinfo;


char printMenu()
{
    char oper;

    do
    {
        printf("PleaseSelect which led: \n"
            "\t[A] led1 on\n"
            "\t[a] led1 off\n"
            "\t[B] led2 on\n"
            "\t[b] led2 off\n"
            "\t[C] led4 on\n"
            "\t[c] led4 off\n"
            "\t[T] led all on\n"
            "\t[t] led all off\n"
            "\t[q|Q] quit appp\n");

        printf("Your selection: ");
        oper = (char)getchar();
        getchar();
        printf("\n");
        if(oper != 'A' && oper != 'a' 
            && oper != 'B' && oper != 'b'  
            && oper != 'C' && oper != 'c'
            && oper != 'T' && oper != 't'
            && oper != 'Q' && oper != 'q')
            continue;
        break;
    }while(1);

    return oper;
}
int main(int argc,char *argv[])
{
    devinfo dinfo = {
        .which = 1,
        .onoff = 0,
    };
	int fd = open("/dev/led", O_RDWR);
	if(fd < 0)
	{
		printf("open [%s] failed\n", "/dev/led");
		return -1;
	}
    int quitApp = 0;
    do
    {
        char oper = printMenu();
        switch(oper)
        {
            case 'A':
                dinfo.which = 1;
                dinfo.onoff = 0;
                break;
            case 'a':
                dinfo.which = 1;
                dinfo.onoff = 1;
                break;
            case 'B':
                dinfo.which = 2;
                dinfo.onoff = 0;
                break;
            case 'b':
                dinfo.which = 2;
                dinfo.onoff = 1;
                break;
            case 'C':
                dinfo.which = 4;
                dinfo.onoff = 0;
                break;
            case 'c':
                dinfo.which = 4;
                dinfo.onoff = 1;
                break;
            case 'T':
                dinfo.which = 3;
                dinfo.onoff = 0;
                break;
            case 't':
                dinfo.which = 3;
                dinfo.onoff = 1;
                break;
           case 'Q':
           case 'q':
                quitApp = 1;
                break;
        }
        if(quitApp)
            break;
        //write(fd, (void *)&dinfo, sizeof(devinfo));
        ioctl(fd, CTRL_LED_CMD, &dinfo);
    }while(1);

	close(fd);
	return 0;
}
